﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entitty;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblImageDAO
    {
        private DBService _db = null;

        public TblImageDAO(string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }

        // lay hinh anh tuong ung voi id
        public TblImage GetImageByID(int idNoidung, int ID)
         {

             TblImage image = new TblImage();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblImage_Lst",
                    new string[] { "@p_idNoidung", "@ID" },
                    new object[] { idNoidung, ID },
                   CommandType.StoredProcedure);

                 foreach (DataRow dr in dt.Rows)
                 {
                     image.ID = Convert.ToInt32(dr["ID"].ToString());
                     image.ImageURL = dr["ImageURL"].ToString();
                     image.IsDefault = Convert.ToBoolean(dr["IsDefault"].ToString());
                     image.IdNoidung = Convert.ToInt32(dr["idNoidung"].ToString());

                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return image;
         }

        // Lay Image
        public DataTable GetImgItems(int IdNoidung)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblImage where IdNoidung = @IdNoidung",
                    new string[] { "@IdNoidung" },
                    new object[] { IdNoidung },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public bool InsImage(TblImage imgNoidung, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblImage_Ins",
                    new string[] { "@p_idNoidung", "@p_ImageURL" },
                    new object[] { imgNoidung.IdNoidung, imgNoidung.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool DeleteImgItem(TblImage img)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblImage	WHERE ID = @p_ID",
                    new string[] { "@p_ID" },
                    new object[] { img.ID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

    }
}
